{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import tensorflow as tf\n",
    "from tensorflow import keras\n",
    "\n",
    "def create_model():\n",
    "  model = tf.keras.Sequential([\n",
    "    tf.keras.layers.Flatten(input_shape=(28, 28)),\n",
    "    tf.keras.layers.Dense(128, activation='relu'),\n",
    "    tf.keras.layers.Dense(10)\n",
    "  ])\n",
    "  model.compile(optimizer='adam',\n",
    "    loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),\n",
    "    metrics=[tf.keras.metrics.SparseCategoricalAccuracy()])\n",
    "  return model\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tensorflow.python.checkpoint.checkpoint.CheckpointLoadStatus at 0x24e6db63910>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model = create_model()\n",
    "model.load_weights('./mnist_checkpoint/')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<keras.engine.sequential.Sequential object at 0x0000024E570582E0>\n"
     ]
    }
   ],
   "source": [
    "print(model)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(28, 28)\n"
     ]
    }
   ],
   "source": [
    "from PIL import Image\n",
    "import numpy as np\n",
    "png_path = \"./db/num.png\"\n",
    "png = Image.open(png_path)\n",
    "png = png.convert(\"L\")\n",
    "print(png.size)\n",
    "dt = np.zeros((28, 28), dtype=int)\n",
    "# print(dt)\n",
    "for y in range(png.size[1]):\n",
    "    for x in range(png.size[0]):\n",
    "        pixel = png.getpixel((x, y))\n",
    "        dt[y][x] = 255 - pixel\n",
    "\n",
    "dt = dt / 255.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "probability_model = tf.keras.Sequential([model, \n",
    "                                         tf.keras.layers.Softmax()])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "mnist = tf.keras.datasets.mnist\n",
    "(train_images, train_labels), (test_images, test_labels) = mnist.load_data()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 28, 28)"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.array([dt]).shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1/1 [==============================] - 0s 32ms/step\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([0.14983937, 0.03272498, 0.12795044, 0.0546676 , 0.05291012,\n",
       "       0.07116807, 0.06917935, 0.05472748, 0.3077221 , 0.0791105 ],\n",
       "      dtype=float32)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "predictions = probability_model.predict(np.array([dt]))\n",
    "predictions[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.argmax(predictions[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "from matplotlib import pyplot as plt\n",
    "def plot_image(t_l, data, prediction_array):\n",
    "  true_label, img = t_l, data\n",
    "  plt.grid(False)\n",
    "  plt.xticks([])\n",
    "  plt.yticks([])\n",
    "\n",
    "  plt.imshow(img, cmap=plt.cm.binary)\n",
    "\n",
    "  predicted_label = np.argmax(prediction_array)\n",
    "  if predicted_label == true_label:\n",
    "    color = 'blue'\n",
    "  else:\n",
    "    color = 'red'\n",
    "\n",
    "  plt.xlabel(\"{} {:2.0f}% ({})\".format(predicted_label,\n",
    "                                100*np.max(prediction_array),\n",
    "                                predicted_label),\n",
    "                                color=color)\n",
    "\n",
    "def plot_value_array(t_l, prediction_array):\n",
    "  true_label = t_l\n",
    "  plt.grid(False)\n",
    "  plt.xticks(range(10))\n",
    "  plt.yticks([])\n",
    "  thisplot = plt.bar(range(10), prediction_array, color=\"#777777\")\n",
    "  plt.ylim([0, 1])\n",
    "  predicted_label = np.argmax(prediction_array)\n",
    "\n",
    "  thisplot[predicted_label].set_color('red')\n",
    "  thisplot[true_label].set_color('blue')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAekAAAESCAYAAADZmy1NAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjYuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/P9b71AAAACXBIWXMAAA9hAAAPYQGoP6dpAAAVx0lEQVR4nO3dfXBU5fnG8StEQyhseAcTIQlBSHiVQESR9odW2g5FyuiMoJPpBNJqHcMIUhko2FLKAH1RKcWiQtvUFpFSC5ZSOpECDTIqUCTW6ABSeQkDgozEBFJAsvfvj2OABLK7ObshD+T7mdmRPWfPvfcicOU55zzPxpmZCQAAOKdFUzcAAACujJAGAMBRhDQAAI4ipAEAcBQhDQCAowhpAAAcRUgDAOCoG5q6AQDXhmAwqCNHjigQCCguLq6p2wGuaWamyspKpaSkqEWL+sfLhDSAiBw5ckTdu3dv6jaA60pZWZm6detW735CGkBEAoGAJO8flaSkpCbuBri2VVRUqHv37hf+XtWHkAYQkZpT3ElJSYQ0ECPhLh1x4xgAAI4ipAEAcBQhDQCAowhpAAAcRUgDAOAoQhoAAEf5noLF6kNAbES68hCA5sd3S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523bt8+sVy+ziorI31sy+/jj2tsfftgsJ8fs8GGzYNBs0yazNm3Miotrv276dLMRIyJ7LwDAZRhJN6YZM7w5d1lZUny8NzKdN887FRxOt27SJ594p6d//GPpu9+N/H379JHmz5e+9jXv+YIF3raRI6Wf/1wqKvJq3nijtGiRN7K+koMHvb67dKm9ffFib/TcrZt3yrxFC2nZssvrpKR4NQAAvhDSjWnVKu9U8YoV3nXZkhJpyhQvvPLyQh/7xhveDVpvv+2F/S23SA89FPl7P/qo96jx0ktSICANGyZlZko7dkiHD0sPPijt3y9d6dTN//7nba873WbxYq+vtWultDTvRraCAu9zjRx58XWtWklVVZH3DACohZBuTNOmeQH74IPe8wEDvJHlggXhQ7pHj4vHHDvmjXwbEtKXOnFCmjPHC9Nt27zpUb16eY/PP5f27vXep65OnbyQPXfOu2Nb8oJ75kxpzZqL19oHDvR+AHn66doh/emnUufO/noGADAFq1FVVXmngi8VHy8Fgw2rEwxK0Syh98QT3qNbN++U++efX9x3/ry37UoGDfL++8EHF7d9/rn3iORzlZbWvgkOANAgjKQb05gx3jXo1FTvdPeuXdKzz3pTp+rz6197r8/K8p5v2eKNUOvOU9637+Lz/fu9kWyHDt6xl9qwwRspv/SS9/y227w7vf/xD6mszAvXzMwr99K5szR4sDcNrCawk5KkESO8swStWnmnu4uLpT/8wftsl3rjDe7sBoBoNPWda9e1igqzyZPNUlPNEhPNMjLMZs0yO3u2/mN+9Suzfv3MvvQls6Qks+xssyVLzKqrL75m82bvruu6j7y82rWqqsx69zbbtav29mXLvDvGU1PN1q0L/RmWLDG7447a244eNZswwSwlxftcmZlmzzzj3eld4803vbvKq6pC10ez8txzz1laWpq1bNnShg4datu2bfNdq7i42O69915LTk42Sbam7iyHBpg/f77l5ORYmzZtrHPnzjZ27FjbvXu3r1pLliyxAQMGWCAQsEAgYHfccYetX7/ed291LViwwCTZ5MmTfR0/e/Zsk1TrkZmZ6bufw4cPW25urnXo0MESExOtf//+tmPHDl+10tLSLutNkj322GMNrnX+/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2rRpysrK8v1nOCKNdt84ANSxePFiS01NtYSEBBs6dKi9/fbbvmtt3rz5igtf5NVd1CcCV6ojyQoLCxtcKz8/39LS0iwhIcE6d+5s99xzj73++usNrhNKNFOwxo8fb8nJyZaQkGA333yzjR8/3ve8ZjOzv/3tb9a/f39r2bKlZWVl2dKlS33XMjMrKioySban5it+faq78EhGRobNmjXLzoZaTCqMP/3pT5aRkWEJCQl20003WUFBgZWXl0fVZzhxZmaN9yMAAADwi2vSAAA4ipAGAMBRhDQAAI4ipAEAcBQhDQCAowhpAAAcRUgDAOAoQhoAAEcR0gAAOIqQBgDAUYQ0AACO+n8xQ7bj8eyCLQAAAABJRU5ErkJggg==",
      "text/plain": [
       "<Figure size 600x300 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "i = 0\n",
    "plt.figure(figsize=(6,3))\n",
    "plt.subplot(1,2,1)\n",
    "plot_image(6, dt, predictions[0])\n",
    "plt.subplot(1,2,2)\n",
    "plot_value_array(6, predictions[0])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = tf.keras.models.load_model('./mnist_checkpoint/')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model: \"sequential_1\"\n",
      "_________________________________________________________________\n",
      " Layer (type)                Output Shape              Param #   \n",
      "=================================================================\n",
      " flatten_1 (Flatten)         (None, 784)               0         \n",
      "                                                                 \n",
      " dense_2 (Dense)             (None, 128)               100480    \n",
      "                                                                 \n",
      " dense_3 (Dense)             (None, 10)                1290      \n",
      "                                                                 \n",
      "=================================================================\n",
      "Total params: 101,770\n",
      "Trainable params: 101,770\n",
      "Non-trainable params: 0\n",
      "_________________________________________________________________\n"
     ]
    }
   ],
   "source": [
    "model.summary()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "ename": "RuntimeError",
     "evalue": "You must compile your model before training/testing. Use `model.compile(optimizer, loss)`.",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mRuntimeError\u001b[0m                              Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[25], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m loss, acc \u001b[39m=\u001b[39m probability_model\u001b[39m.\u001b[39;49mevaluate(train_images, train_labels, verbose\u001b[39m=\u001b[39;49m\u001b[39m2\u001b[39;49m)\n",
      "File \u001b[1;32mc:\\Users\\FanChenChen\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\keras\\utils\\traceback_utils.py:70\u001b[0m, in \u001b[0;36mfilter_traceback.<locals>.error_handler\u001b[1;34m(*args, **kwargs)\u001b[0m\n\u001b[0;32m     67\u001b[0m     filtered_tb \u001b[39m=\u001b[39m _process_traceback_frames(e\u001b[39m.\u001b[39m__traceback__)\n\u001b[0;32m     68\u001b[0m     \u001b[39m# To get the full stack trace, call:\u001b[39;00m\n\u001b[0;32m     69\u001b[0m     \u001b[39m# `tf.debugging.disable_traceback_filtering()`\u001b[39;00m\n\u001b[1;32m---> 70\u001b[0m     \u001b[39mraise\u001b[39;00m e\u001b[39m.\u001b[39mwith_traceback(filtered_tb) \u001b[39mfrom\u001b[39;00m \u001b[39mNone\u001b[39m\n\u001b[0;32m     71\u001b[0m \u001b[39mfinally\u001b[39;00m:\n\u001b[0;32m     72\u001b[0m     \u001b[39mdel\u001b[39;00m filtered_tb\n",
      "File \u001b[1;32mc:\\Users\\FanChenChen\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\keras\\engine\\training.py:3690\u001b[0m, in \u001b[0;36mModel._assert_compile_was_called\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m   3684\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39m_assert_compile_was_called\u001b[39m(\u001b[39mself\u001b[39m):\n\u001b[0;32m   3685\u001b[0m     \u001b[39m# Checks whether `compile` has been called. If it has been called,\u001b[39;00m\n\u001b[0;32m   3686\u001b[0m     \u001b[39m# then the optimizer is set. This is different from whether the\u001b[39;00m\n\u001b[0;32m   3687\u001b[0m     \u001b[39m# model is compiled\u001b[39;00m\n\u001b[0;32m   3688\u001b[0m     \u001b[39m# (i.e. whether the model is built and its inputs/outputs are set).\u001b[39;00m\n\u001b[0;32m   3689\u001b[0m     \u001b[39mif\u001b[39;00m \u001b[39mnot\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_is_compiled:\n\u001b[1;32m-> 3690\u001b[0m         \u001b[39mraise\u001b[39;00m \u001b[39mRuntimeError\u001b[39;00m(\n\u001b[0;32m   3691\u001b[0m             \u001b[39m\"\u001b[39m\u001b[39mYou must compile your model before \u001b[39m\u001b[39m\"\u001b[39m\n\u001b[0;32m   3692\u001b[0m             \u001b[39m\"\u001b[39m\u001b[39mtraining/testing. \u001b[39m\u001b[39m\"\u001b[39m\n\u001b[0;32m   3693\u001b[0m             \u001b[39m\"\u001b[39m\u001b[39mUse `model.compile(optimizer, loss)`.\u001b[39m\u001b[39m\"\u001b[39m\n\u001b[0;32m   3694\u001b[0m         )\n",
      "\u001b[1;31mRuntimeError\u001b[0m: You must compile your model before training/testing. Use `model.compile(optimizer, loss)`."
     ]
    }
   ],
   "source": [
    "loss, acc = probability_model.evaluate(train_images, train_labels, verbose=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "probability_model = tf.keras.Sequential([model, \n",
    "                                            tf.keras.layers.Softmax()])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1875/1875 [==============================] - 3s 1ms/step\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([0.0000000e+00, 1.5542633e-23, 0.0000000e+00, 2.7120488e-21,\n",
       "       0.0000000e+00, 1.0000000e+00, 0.0000000e+00, 0.0000000e+00,\n",
       "       5.4433098e-34, 0.0000000e+00], dtype=float32)"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pre = probability_model.predict(train_images)\n",
    "pre[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3 3\n"
     ]
    }
   ],
   "source": [
    "print(np.argmax(pre[30]), train_labels[30])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "ee21c858e9e6cc3714526aa083051029b46bcc72805c0fea5b8193e35926ec97"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
